﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Newtonsoft.Json;
using webapi.DTO;
using webapi.Helper;
using webapi.Model;

namespace webapi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    [Authorize]
    public class ProductController : ControllerBase
    {
        private readonly ILogger<ProductController> _logger;

        private DbContextHelper _dbContextHelper;

        private HashHelper _hashHelper;

        public ProductController(ILogger<ProductController> logger, DbContextHelper dbContextHelper)
        {
            _logger = logger;
            _dbContextHelper = dbContextHelper;
            _hashHelper = new HashHelper();
        }

        [HttpPost("GetProductsFilter")]
        [AllowAnonymous]
        public ProductsFilterOutputDTO GetProductsFIlter([FromBody] ProductsFilterInputDTO input)
        {
            List<String> list = new List<String>();
            if (input.filterType == "year")
            {
                list = _dbContextHelper.ProductDetail.Where(p => p.DELETE_DATE == null).Select(b => b.YEAR).Distinct().ToList();
            }
            else if (input.filterType == "brand")
            {
                list = _dbContextHelper.ProductDetail.Where(p => p.DELETE_DATE == null).Where(b => b.YEAR == input.year).Select(b => b.BRAND).Distinct().ToList();
            }
            else if (input.filterType == "model")
            {
                list = _dbContextHelper.ProductDetail.Where(p => p.DELETE_DATE == null).Where(b => b.YEAR == input.year).Where(b => b.BRAND == input.brand).Select(b => b.MODEL).Distinct().ToList();
            }
            else if (input.filterType == "engine")
            {
                list = _dbContextHelper.ProductDetail.Where(p => p.DELETE_DATE == null).Where(b => b.YEAR == input.year).Where(b => b.BRAND == input.brand).Where(b => b.MODEL == input.model).Select(b => b.ENGINE).Distinct().ToList();
            }
            else if (input.filterType == "fuelType")
            {
                list = _dbContextHelper.ProductDetail.Where(p => p.DELETE_DATE == null).Where(b => b.YEAR == input.year).Where(b => b.BRAND == input.brand).Where(b => b.MODEL == input.model).Where(b => b.ENGINE == input.engine).Select(b => b.FUEL_TYPE).Distinct().ToList();
            }

            return new ProductsFilterOutputDTO()
            {
                data = list,
                error_code = "2000",
                status = "200",
                msg = "",
            };
        }

        [HttpPost("GetAllProducts")]
        [AllowAnonymous]
        public ProductsOutputDTO GetAllProducts([FromBody] InputDTO input)
        {
            List<ProductInfoModel> listLine = new List<ProductInfoModel>();
            //List<int> productIdList = new List<int>();
            List<Product> data = new List<Product>();
            ProductAllModel listAllProducts = new ProductAllModel();
            int productNum = 0;

            int listCount = _dbContextHelper.ProductInfo.Where(p => p.DELETE_DATE == null).Count();
            listLine = _dbContextHelper.ProductInfo.Where(p => p.DELETE_DATE == null).Skip(input.pageSize * (input.pageNum - 1)).Take(input.pageSize).OrderByDescending(p => p.PRODUCT_ID).ToList();

            //int listCount = _dbContextHelper.ProductInfo.Where(p => p.DELETE_DATE == null).Where(b => b.YEAR == input.year).Where(b => b.BRAND == input.brand).Where(b => b.MODEL == input.model).Where(b => b.ENGINE == input.engine).Where(b => b.FUEL_TYPE == input.fuelType).Count();

            //listLine = _dbContextHelper.ProductInfo.Where(p => p.DELETE_DATE == null).Where(p => productIdList.Contains(p.PRODUCT_ID)).ToList();

            foreach (var item in listLine)
            {
                Product product = new Product();
                listAllProducts = _dbContextHelper.Product.Where(p => p.DELETE_DATE == null).Where(b => b.PRODUCT_INFO_GUID == item.GUID).FirstOrDefault();
                product.product_id = item.PRODUCT_ID.ToString();
                product.product_name = item.PRODUCT_NAME;
                product.product_description = item.PRODUCT_DESCRIPTION;
                product.product_detail = item.PRODUCT_DETAIL;
                product.product_num = item.PRODUCT_NUM;
                product.product_price = item.PRODUCT_PRICE;
                product.product_media1 = item.PRODUCT_MEDIA1;
                product.product_media2 = item.PRODUCT_MEDIA2;
                product.product_media3 = item.PRODUCT_MEDIA3;
                product.product_media4 = item.PRODUCT_MEDIA4;
                if (listAllProducts != null)
                {
                    product.upload_datetime = listAllProducts.UPLOAD_DATETIME.ToString();
                    product.delete_date = listAllProducts.DELETE_DATE != null ? listAllProducts.DELETE_DATE.ToString() : null;
                }
                data.Add(product);
            }
            var result = new ProductsOutputDTO()
            {
                data = data,
                page_count = listCount % input.pageSize == 0 ? (int)(listCount / input.pageSize) : (int)(listCount / input.pageSize) + 1,
                error_code = "2000",
                status = "200",
                msg = ""
            };
            return result;
        }

        [HttpPost("GetProducts")]
        [AllowAnonymous]
        public ProductsOutputDTO GetProducts([FromBody] ProductsInputDTO input)
        {
            List<ProductInfoModel> listLine = new List<ProductInfoModel>();
            List<int> productIdList = new List<int>();
            List<Product> data = new List<Product>();
            ProductAllModel listAllProducts = new ProductAllModel();
            int productNum = 0;

            int listCount = _dbContextHelper.ProductDetail.Where(p => p.DELETE_DATE == null).Where(b => b.YEAR == input.year).Where(b => b.BRAND == input.brand).Where(b => b.MODEL == input.model).Where(b => b.ENGINE == input.engine).Where(b => b.FUEL_TYPE == input.fuelType).Count();
            productIdList = _dbContextHelper.ProductDetail.Where(p => p.DELETE_DATE == null).Where(b => b.YEAR == input.year).Where(b => b.BRAND == input.brand).Where(b => b.MODEL == input.model).Where(b => b.ENGINE == input.engine).Where(b => b.FUEL_TYPE == input.fuelType).Skip(input.pageSize * (input.pageNum - 1)).Take(input.pageSize).Select(p => p.PRODUCT_ID).ToList();

            //int listCount = _dbContextHelper.ProductInfo.Where(p => p.DELETE_DATE == null).Where(b => b.YEAR == input.year).Where(b => b.BRAND == input.brand).Where(b => b.MODEL == input.model).Where(b => b.ENGINE == input.engine).Where(b => b.FUEL_TYPE == input.fuelType).Count();

            listLine = _dbContextHelper.ProductInfo.Where(p => p.DELETE_DATE == null).Where(p => productIdList.Contains(p.PRODUCT_ID)).ToList();

            foreach (var item in listLine)
            {
                Product product = new Product();
                listAllProducts = _dbContextHelper.Product.Where(p => p.DELETE_DATE == null).Where(b => b.PRODUCT_INFO_GUID == item.GUID).FirstOrDefault();
                product.product_id = item.PRODUCT_ID.ToString();
                product.product_name = item.PRODUCT_NAME;
                product.product_description = item.PRODUCT_DESCRIPTION;
                product.product_detail = item.PRODUCT_DETAIL;
                product.product_num = item.PRODUCT_NUM;
                product.product_price = item.PRODUCT_PRICE;
                product.product_media1 = item.PRODUCT_MEDIA1;
                product.product_media2 = item.PRODUCT_MEDIA2;
                product.product_media3 = item.PRODUCT_MEDIA3;
                product.product_media4 = item.PRODUCT_MEDIA4;
                //product.year = item.YEAR;
                //product.brand = item.BRAND;
                //product.model = item.MODEL;
                //product.engine = item.ENGINE;
                //product.fuel_type = item.FUEL_TYPE;
                if (listAllProducts != null)
                {
                    product.upload_datetime = listAllProducts.UPLOAD_DATETIME.ToString();
                    product.delete_date  = listAllProducts.DELETE_DATE != null ? listAllProducts.DELETE_DATE.ToString() : null;
                }
                data.Add(product);
            }
            var result = new ProductsOutputDTO()
            {
                data = data,
                page_count = listCount % input.pageSize == 0? (int)(listCount / input.pageSize) : (int)(listCount / input.pageSize) + 1,
                error_code = "2000",
                status = "200",
                msg = ""
            };
            return result;
        }

        [HttpPost("GetProductInfo")]
        [AllowAnonymous]
        public GetProductInfoOutput GetProductInfo([FromBody] GetProductInfoInput input)
        {
            var productId = int.Parse(input.productId);
            ProductInfoModel productInfo = _dbContextHelper.ProductInfo.Where(p => p.DELETE_DATE == null).Where(b => b.PRODUCT_ID == productId).FirstOrDefault();
            Product product = new Product();
            if (productInfo != null)
            {
                product.product_id = productInfo.PRODUCT_ID.ToString();
                product.product_name = productInfo.PRODUCT_NAME;
                product.product_description = productInfo.PRODUCT_DESCRIPTION;
                product.product_detail = productInfo.PRODUCT_DETAIL;
                product.product_num = productInfo.PRODUCT_NUM;
                product.product_price = productInfo.PRODUCT_PRICE;
                product.product_media1 = productInfo.PRODUCT_MEDIA1;
                product.product_media2 = productInfo.PRODUCT_MEDIA2;
                product.product_media3 = productInfo.PRODUCT_MEDIA3;
                product.product_media4 = productInfo.PRODUCT_MEDIA4;
                //product.year = productInfo.YEAR;
                //product.brand = productInfo.BRAND;
                //product.model = productInfo.MODEL;
                //product.engine = productInfo.ENGINE;
                //product.fuel_type = productInfo.FUEL_TYPE;
                ProductAllModel productModel = _dbContextHelper.Product.Where(p => p.DELETE_DATE == null).Where(b => b.PRODUCT_INFO_GUID == productInfo.GUID).FirstOrDefault();
                if (productModel != null)
                {
                    product.upload_datetime = productModel.UPLOAD_DATETIME.ToString();
                    product.delete_date = productModel.DELETE_DATE != null ? productModel.DELETE_DATE.ToString() : null;
                }

                return new GetProductInfoOutput()
                {
                    data = product,
                    error_code = "2000",
                    status = "200",
                    msg = ""
                };
            }
            return new GetProductInfoOutput()
            {
                data = product,
                error_code = "2001",
                status = "200",
                msg = ""
            };
        }
    }
}
